package cn.cw.itsource.result;

import lombok.Data;

import java.io.Serializable;

/**
 * 后台返回前端统一结果对象
 */
@Data
public class JSONResult implements Serializable {

    private boolean success = true;

    private String message = ErrorCode.SYSTEM_SUCCESS.getMessage();

    //成功统一返回0000，其余编码全部是错误码
    private String code = ErrorCode.SYSTEM_SUCCESS.getCode();

    //是否展示提示信息（默认不展示）
    private boolean showMessage = false;

    //返回的数据
    private Object data;

    //创建当前实例
    public static JSONResult success() {
        return new JSONResult();
    }

    //创建当前实例
    public static JSONResult success(String message) {
        JSONResult instance = new JSONResult();
        instance.setMessage(message);
        return instance;
    }

    //创建当前实例
    public static JSONResult success(boolean showMessage) {
        JSONResult instance = new JSONResult();
        instance.setShowMessage(showMessage);
        return instance;
    }

    //创建当前实例
    public static JSONResult success(Object obj) {
        JSONResult instance = new JSONResult();
        instance.setData(obj);
        return instance;
    }

    //创建当前实例
    public static JSONResult success(Object obj, boolean showMessage) {
        JSONResult instance = new JSONResult();
        instance.setData(obj);
        instance.setShowMessage(showMessage);
        return instance;
    }

    //成功，但是返回不同消息代码
    public static JSONResult success(Object obj, String code) {
        JSONResult instance = new JSONResult();
        instance.setSuccess(true);
        instance.setCode(code);
        instance.setData(obj);
        return instance;
    }

    public static JSONResult success(String code, String message) {
        JSONResult instance = new JSONResult();
        instance.setSuccess(true);
        instance.setCode(code);
        instance.setMessage(message);
        return instance;
    }

    //创建当前实例
    public static JSONResult error() {
        JSONResult instance = new JSONResult();
        instance.setCode(ErrorCode.SYSTEM_ERROR.getCode());
        instance.setSuccess(false);
        instance.setMessage(ErrorCode.SYSTEM_ERROR.getMessage());
        return instance;
    }

    //创建当前实例
    public static JSONResult error(String message) {
        JSONResult instance = new JSONResult();
        instance.setCode(ErrorCode.SYSTEM_ERROR.getCode());
        instance.setSuccess(false);
        instance.setMessage(message);
        return instance;
    }

    public static JSONResult error(String message, Object obj) {
        JSONResult instance = new JSONResult();
        instance.setCode(ErrorCode.SYSTEM_ERROR.getCode());
        instance.setMessage(message);
        instance.setSuccess(false);
        instance.setData(obj);
        return instance;
    }

    public static JSONResult error(String code, String message) {
        JSONResult instance = new JSONResult();
        instance.setCode(code);
        instance.setMessage(message);
        instance.setSuccess(false);
        return instance;
    }

    //接收一个错误码的枚举
    public static JSONResult error(ErrorCode errorCode) {
        JSONResult instance = new JSONResult();
        instance.setMessage(errorCode.getMessage());
        instance.setSuccess(false);
        instance.setCode(errorCode.getCode());
        return instance;
    }
}
